﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Drawing;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Havivi.FootballPool.MatchesWebPart
{
    [ToolboxItemAttribute(false)]
    public class MatchesWebPart : WebPart
    {
        private bool showBackground;
        public List<Label> Labels { get; set; }

        private string GetValueFromKey(string key)
        {
            try
            {
                foreach (var label in Labels.Where(label => label.Key == key))
                {
                    return label.Value;
                }
                return string.Empty;
            }
            catch
            {
                return string.Empty;
            }

        }

        [WebBrowsable(true),
  WebDescription("Show background for higher resolutions"),
  WebDisplayName("Show background"),
  Personalizable(PersonalizationScope.Shared)]
        public bool ShowBackground
        {
            get
            {
                return showBackground;
            }

            set
            {
                showBackground = value;
            }
        }
        protected override void CreateChildControls()
        {
            try
            {
                var web = SPContext.Current.Site.RootWeb;
                GetLabels(web);
                string viewUser = string.Empty;
                if (Page.Request.QueryString["euroUser"] != null)
                    viewUser = Page.Request.QueryString["euroUser"].ToString();
                var siteUrl = SPContext.Current.Site.Url;
                Table tableMain;
                if (ShowBackground)
                {
                    tableMain = new Table
                                          {
                                              BorderStyle = BorderStyle.Solid,
                                              BorderWidth = Unit.Pixel(1),
                                              Width = Unit.Pixel(1280),
                                              BackImageUrl =
                                                  siteUrl + "/SiteAssets/Havivi.WK2014/Images/poolbg.jpg"
                                          };
                }
                else
                {
                    tableMain = new Table
                    {
                        BorderStyle = BorderStyle.Solid,
                        BorderWidth = Unit.Pixel(1),
                    };
                }
                tableMain.Style.Add("background-repeat", "no-repeat");
                CreateMainHeader(tableMain, siteUrl, viewUser);
                var rowMain = CreateEmptyCell();
                var cellMain = new TableCell { HorizontalAlign = HorizontalAlign.Left };
                var table = new Table { CssClass = "ms-listviewtable", CellPadding = 0, CellSpacing = 0 };
                CreateHeader(table);
                var itemsUserMatches = GetUserMatches(web, viewUser);
                var items = GetMatches(web);
                var alternatingstrong = false;
                var matchIndex = 0;
                foreach (SPListItem item in items)
                {
                    matchIndex++;
                    var row = new TableRow();
                    if (alternatingstrong)
                        row.CssClass = "ms-alternatingstrong";
                    else
                        row.BackColor = Color.White;
                    CreateMatchRow(matchIndex, row, item, itemsUserMatches, siteUrl, viewUser);
                    switch (matchIndex)
                    {
                        case 49:
                            CreateEigthFinals(table);
                            break;
                        case 57:
                            CreateQuarterFinals(table);
                            break;
                        case 61:
                            CreateSemiFinals(table);
                            break;
                        case 63:
                            CreateTroostFinal(table);
                            break;
                        case 64:
                            CreateFinal(table);
                            break;
                    }
                    table.Rows.Add(row);
                    alternatingstrong = !alternatingstrong;
                }
                if (string.IsNullOrEmpty(viewUser))
                    CreateButtonRow(table);
                cellMain.Controls.Add(table);
                rowMain.Cells.Add(cellMain);
                tableMain.Rows.Add(rowMain);
                Controls.Add(tableMain);


                this.Title = GetValueFromKey("WK2014Matches");
            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl(ex.Message));
            }
        }

        private static void CreateMatchRow(int matchIndex, TableRow row, SPListItem item, SPListItemCollection itemsUserMatches, string siteUrl, string viewUser)
        {
            CreateIdCell(item, row);
            CreateDateCell(row, item);
            CreateTeamACell(matchIndex, row, item, itemsUserMatches, siteUrl, viewUser);
            CreateMinCell(row);
            CreateTeamBCell(matchIndex, row, item, itemsUserMatches, siteUrl, viewUser);

            var cell = new TableCell { HorizontalAlign = HorizontalAlign.Center };
            cell.Controls.Add(new LiteralControl("<SPAN class=menu-item-text>"));
            if (item["ScoreA"] != null)
                cell.Controls.Add(new LiteralControl(item["ScoreA"].ToString()));
            cell.Controls.Add(new LiteralControl("-"));
            if (item["ScoreB"] != null)
                cell.Controls.Add(new LiteralControl(item["ScoreB"].ToString()));
            cell.Controls.Add(new LiteralControl("</SPAN>"));
            row.Cells.Add(cell);

            cell = new TableCell { HorizontalAlign = HorizontalAlign.Center };
            if (itemsUserMatches.Count > 0)
                if (itemsUserMatches[matchIndex - 1]["Score"] != null)
                    cell.Controls.Add(new LiteralControl(string.Format("<SPAN class=menu-item-text>{0}</SPAN>", itemsUserMatches[matchIndex - 1]["Score"].ToString())));
            row.Cells.Add(cell);



            cell = new TableCell();
            cell.Controls.Add(new LiteralControl("&nbsp&nbsp"));
            row.Cells.Add(cell);
        }

        private static void CreateTeamBCell(int matchIndex, TableRow row, SPListItem item, SPListItemCollection itemsUserMatches, string siteUrl, string viewUser)
        {
            var cell = new TableCell { CssClass = "ms-vb2", HorizontalAlign = HorizontalAlign.Left };
            var ddl = CreateDropDownList("HaviviTeamScore2_", "ScoreTeamB", matchIndex, item, itemsUserMatches, viewUser);
            cell.Controls.Add(ddl);
            if (item["FlagB"] != null)
                cell.Controls.Add(new LiteralControl("&nbsp&nbsp<img src=\"" + siteUrl + "/SiteAssets/Havivi.WK2014/Images/flags/" + item["FlagB"].ToString() + "\">&nbsp&nbsp"));
            if (item["TeamB"] != null)
                cell.Controls.Add(new LiteralControl(item["TeamB"].ToString()));
            row.Cells.Add(cell);
        }

        private static void CreateMinCell(TableRow row)
        {
            var cell = new TableCell();
            cell.Controls.Add(new LiteralControl(" - &nbsp"));
            row.Cells.Add(cell);
        }

        private static void CreateTeamACell(int matchIndex, TableRow row, SPListItem item, SPListItemCollection itemsUserMatches, string siteUrl, string viewUser)
        {
            var cell = new TableCell { CssClass = "ms-vb2", HorizontalAlign = HorizontalAlign.Right };
            if (item["TeamA"] != null)
                cell.Controls.Add(new LiteralControl(item["TeamA"].ToString()));
            if (item["FlagA"] != null)
                cell.Controls.Add(new LiteralControl("&nbsp&nbsp<img src=\"" + siteUrl + "/SiteAssets/Havivi.WK2014/Images/flags/" + item["FlagA"].ToString() + "\">&nbsp&nbsp"));
            var ddl1 = CreateDropDownList("HaviviTeamScore1_", "ScoreTeamA", matchIndex, item, itemsUserMatches, viewUser);
            cell.Controls.Add(ddl1);
            row.Cells.Add(cell);
        }

        private static void CreateDateCell(TableRow row, SPListItem item)
        {
            var cell = new TableCell { HorizontalAlign = HorizontalAlign.Left, CssClass = "ms-vb2" };
            if (item["MatcheDate"] != null)
            {
                var mDate = (DateTime)item["MatcheDate"];;
                cell.Controls.Add(new LiteralControl("&nbsp&nbsp&nbsp&nbsp" + mDate.ToLongDateString() + " " + mDate.ToShortTimeString()));
            }
            row.Cells.Add(cell);
        }

        private static void CreateIdCell(SPListItem item, TableRow row)
        {
            var cell = new TableCell { HorizontalAlign = HorizontalAlign.Center };
            cell.Controls.Add(new LiteralControl(item["ID"].ToString()));
            row.Cells.Add(cell);
        }

        private TableRow CreateEmptyCell()
        {
            var rowMain = new TableRow();
            if (ShowBackground)
            {

                var cellMain = new TableCell { Width = Unit.Pixel(232) };
                cellMain.Controls.Add(new LiteralControl("&nbsp&nbsp"));
                rowMain.Cells.Add(cellMain);
            }


            return rowMain;
        }

        private void CreateButtonRow(Table table)
        {
            var row = new TableRow();
            var cell = new TableCell { ColumnSpan = 2 };
            row.Cells.Add(cell);

            cell = new TableCell { HorizontalAlign = HorizontalAlign.Right, ColumnSpan = 3 };
            var btn = new Button();
            btn.Click += new EventHandler(this.Save_Click);
            string save = GetValueFromKey("Save");
            btn.Text = save;
            btn.CssClass = "ms-ButtonHeightWidth";
            btn.Width = Unit.Pixel(88);
            cell.Controls.Add(btn);
            cell.Style.Add("padding-right", "196px");
            row.Cells.Add(cell);

            cell = new TableCell { ColumnSpan = 3 };
            row.Cells.Add(cell);
            table.Rows.Add(row);
        }

        private static SPListItemCollection GetMatches(SPWeb web)
        {
            var list = web.Lists["HaviviMatches"];
            var query = new SPQuery
                            {
                                Query = "<OrderBy><FieldRef Name=\"MatcheDate\" Ascending=\"True\" /></OrderBy>",
                                RowLimit = 100
                            };
            return list.GetItems(query);
        }

        private SPListItemCollection GetUserMatches(SPWeb web, string viewUser)
        {
            var listUserMatches = web.Lists["HaviviUserMatches"];
            var queryUserMatches = new SPQuery
                                       {
                                           Query =
                                               string.Format(
                                                   "<Where><Eq><FieldRef Name=\"User\" /><Value Type=\"Text\">{0}</Value></Eq></Where><OrderBy><FieldRef Name=\"Matche\" Ascending=\"True\" /></OrderBy>",
                                                   web.CurrentUser.LoginName),
                                           RowLimit = 100
                                       };

            if (!string.IsNullOrEmpty(viewUser))
                queryUserMatches = new SPQuery
                {
                    Query =
                        string.Format(
                            "<Where><Eq><FieldRef Name=\"DispName\" /><Value Type=\"Text\">{0}</Value></Eq></Where><OrderBy><FieldRef Name=\"Matche\" Ascending=\"True\" /></OrderBy>",
                            viewUser),
                    RowLimit = 100
                };
            return listUserMatches.GetItems(queryUserMatches);
        }

        private void CreateMainHeader(Table tableMain, string url, string viewUser)
        {

            var rowMain = new TableRow();
            var cellMain = new TableCell { Height = Unit.Pixel(24), HorizontalAlign = HorizontalAlign.Left };
            string myPredictions = GetValueFromKey("MyPredictions");
            string userPredictions = GetValueFromKey("Predictions");
            if (ShowBackground)
            {

                if (string.IsNullOrEmpty(viewUser))
                    cellMain.Controls.Add(
                        new LiteralControl(
                            "&nbsp&nbsp<SPAN class=menu-item-text style=\"color: white\">" + myPredictions + "</SPAN>"));
                else
                    cellMain.Controls.Add(
                        new LiteralControl(
                            string.Format(
                                "&nbsp&nbsp<SPAN class=menu-item-text style=\"color: white\">{0} {1}</SPAN>",
                                viewUser, userPredictions)));
                rowMain.Cells.Add(cellMain);
            }

            cellMain = new TableCell { Height = Unit.Pixel(24), HorizontalAlign = HorizontalAlign.Right };//License.aspx
            if (!ShowBackground)
            {
                cellMain.HorizontalAlign = HorizontalAlign.Left;
                if (string.IsNullOrEmpty(viewUser))
                    cellMain.Controls.Add(
                        new LiteralControl(
                            "&nbsp&nbsp<table width=100%><tr><td><SPAN class=menu-item-text  >" + myPredictions + "</SPAN> </td>   "));
                else
                    cellMain.Controls.Add(
                        new LiteralControl(
                            string.Format(
                                "&nbsp&nbsp<table width=100%><tr><td><SPAN class=menu-item-text  >{0} {1}</SPAN></td>  ",
                                viewUser, userPredictions)));
            }
            if (!ShowBackground)
                cellMain.Controls.Add(new LiteralControl("<td align=right> "));
            string rules = GetValueFromKey("Rules");
            string license = GetValueFromKey("License");
            string poweredBy = GetValueFromKey("PoweredBy");
            cellMain.Controls.Add(new LiteralControl("<SPAN class=menu-item-text style=\"color: #676767\"><small>" + string.Format(PopUp, "HaviviRules1", url, "Regels", "/SiteAssets/Havivi.WK2014/Rules.aspx") + " | "));
            cellMain.Controls.Add(new LiteralControl(string.Format(PopUp, "HaviviLicense1", url, "Licentie", "/SiteAssets/Havivi.WK2014/License.aspx") + " | "));
            cellMain.Controls.Add(new LiteralControl(poweredBy + " <a style=\"color: #676767\" href=\"http://www.e-office.com\" target=\"_blank\" >e-office</a>  "));
            if (!ShowBackground)
                cellMain.Controls.Add(new LiteralControl("</td></tr></table> "));
            rowMain.Cells.Add(cellMain);
            tableMain.Rows.Add(rowMain);
        }

        private void CreateHeader(Table table)
        {
            var row = new TableRow { CssClass = "ms-viewheadertr" };
            var cell = new TableCell
                           {
                               HorizontalAlign = HorizontalAlign.Center,
                               CssClass = "ms-vh2-nofilter ms-vh2-gridview"
                           };
            cell.Controls.Add(new LiteralControl("<SPAN class=menu-item-text>#</SPAN>"));
            cell.Width = Unit.Pixel(30);
            row.Cells.Add(cell);
            cell = new TableCell
                       {
                           CssClass = "ms-vh2-nofilter ms-vh2-gridview",
                           HorizontalAlign = HorizontalAlign.Left,
                           Width = Unit.Pixel(254)
                       };
            string time = GetValueFromKey("Time");
            cell.Controls.Add(new LiteralControl("<SPAN class=menu-item-text>&nbsp&nbsp" + time + "</SPAN>"));
            row.Cells.Add(cell);
            cell = new TableCell
                       {
                           Width = Unit.Pixel(230),
                           HorizontalAlign = HorizontalAlign.Right,
                           CssClass = "ms-vh2-nofilter ms-vh2-gridview"
                       };
            string teamA = GetValueFromKey("TeamA");
            cell.Controls.Add(new LiteralControl("<SPAN class=menu-item-text>" + teamA + "</SPAN>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"));
            row.Cells.Add(cell);

            cell = new TableCell { CssClass = "ms-vh2-nofilter ms-vh2-gridview" };
            cell.Controls.Add(new LiteralControl(" "));
            row.Cells.Add(cell);

            cell = new TableCell
                       {
                           Width = Unit.Pixel(230),
                           CssClass = "ms-vh2-nofilter ms-vh2-gridview",
                           HorizontalAlign = HorizontalAlign.Left
                       };
            string teamB = GetValueFromKey("TeamB");
            cell.Controls.Add(new LiteralControl("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<SPAN class=menu-item-text>" + teamB + "</SPAN>"));
            row.Cells.Add(cell);
            table.Rows.Add(row);

            cell = new TableCell { CssClass = "ms-vh2-nofilter ms-vh2-gridview" };
            string score = GetValueFromKey("Score");
            cell.Controls.Add(new LiteralControl("<SPAN class=menu-item-text>" + score + "</SPAN>&nbsp&nbsp"));
            row.Cells.Add(cell);

            cell = new TableCell
                       {
                           HorizontalAlign = HorizontalAlign.Left,
                           CssClass = "ms-vh2-nofilter ms-vh2-gridview"
                       };
            string points = GetValueFromKey("Points");
            cell.Controls.Add(new LiteralControl("<SPAN class=menu-item-text>" + points + "</SPAN>"));
            row.Cells.Add(cell);

            cell = new TableCell { CssClass = "ms-vh2-nofilter ms-vh2-gridview" };
            cell.Controls.Add(new LiteralControl("&nbsp&nbsp"));
            row.Cells.Add(cell);

            row = new TableRow { CssClass = "ms-viewheadertr" };
            cell = new TableCell { ColumnSpan = 8, CssClass = "ms-vh2-nofilter ms-vh2-gridview" };
            string firstRound = GetValueFromKey("FirstRound");
            cell.Controls.Add(new LiteralControl("<H3  class=\"ms-standardheader ms-WPTitle\" style=\"TEXT-ALIGN: justify;font-weight: normal\" ><NOBR><SPAN>&nbsp&nbsp" + firstRound + "</SPAN> </NOBR></H3>"));
            row.Cells.Add(cell);
            table.Rows.Add(row);
        }

        private void CreateFinal(Table table)
        {
            string final = GetValueFromKey("Final");
            var row = new TableRow { CssClass = "ms-viewheadertr" };
            var cell = new TableCell { ColumnSpan = 8, CssClass = "ms-vh2-nofilter ms-vh2-gridview" };
            cell.Controls.Add(new LiteralControl("<H3  class=\"ms-standardheader ms-WPTitle\" style=\"TEXT-ALIGN: justify;font-weight: normal\" ><NOBR><SPAN>&nbsp&nbsp" + final + "</SPAN> </NOBR></H3>"));
            row.Cells.Add(cell);
            table.Rows.Add(row);
        }

        private void CreateTroostFinal(Table table)
        {
            string troostfinal = GetValueFromKey("TroostFinal");
            var row = new TableRow { CssClass = "ms-viewheadertr" };
            var cell = new TableCell { ColumnSpan = 8, CssClass = "ms-vh2-nofilter ms-vh2-gridview" };
            cell.Controls.Add(new LiteralControl("<H3  class=\"ms-standardheader ms-WPTitle\" style=\"TEXT-ALIGN: justify;font-weight: normal\" ><NOBR><SPAN>&nbsp&nbsp"+ troostfinal +"</SPAN> </NOBR></H3>"));
            row.Cells.Add(cell);
            table.Rows.Add(row);
        }

        private   void CreateSemiFinals(Table table)
        {
            string semiFinals = GetValueFromKey("SemiFinals");
            var row = new TableRow { CssClass = "ms-viewheadertr" };
            var cell = new TableCell { ColumnSpan = 8, CssClass = "ms-vh2-nofilter ms-vh2-gridview" };
            cell.Controls.Add(new LiteralControl("<H3  class=\"ms-standardheader ms-WPTitle\" style=\"TEXT-ALIGN: justify;font-weight: normal\" ><NOBR><SPAN>&nbsp&nbsp" + semiFinals + "</SPAN> </NOBR></H3>"));
            row.Cells.Add(cell);
            table.Rows.Add(row);
        }

        private void CreateQuarterFinals(Table table)
        {
            string quarterFinals = GetValueFromKey("QuarterFinals");
            var row = new TableRow { CssClass = "ms-viewheadertr" };
            var cell = new TableCell { ColumnSpan = 8, CssClass = "ms-vh2-nofilter ms-vh2-gridview" };
            cell.Controls.Add(new LiteralControl("<br /><H3  class=\"ms-standardheader ms-WPTitle\" style=\"TEXT-ALIGN: justify;font-weight: normal\" ><NOBR><SPAN>&nbsp&nbsp" + quarterFinals + "</SPAN> </NOBR></H3>"));
            row.Cells.Add(cell);
            table.Rows.Add(row);
        }

        private void CreateEigthFinals(Table table)
        {
            string eightFinals = GetValueFromKey("EightFinals");
            var row = new TableRow { CssClass = "ms-viewheadertr" };
            var cell = new TableCell { ColumnSpan = 8, CssClass = "ms-vh2-nofilter ms-vh2-gridview" };
            cell.Controls.Add(new LiteralControl("<br /><H3  class=\"ms-standardheader ms-WPTitle\" style=\"TEXT-ALIGN: justify;font-weight: normal\" ><NOBR><SPAN>&nbsp&nbsp" + eightFinals + "</SPAN> </NOBR></H3>"));
            row.Cells.Add(cell);
            table.Rows.Add(row);
        }

        private static DropDownList CreateDropDownList(string id, string scoreTeam, int matchIndex, SPListItem item, SPListItemCollection itemsUserMatches, string viewUser)
        {
            var ddl = new DropDownList { ID = id + matchIndex, CssClass = "ms-RadioText" };
            ddl.Items.Add(new ListItem("", ""));
            ddl.Items.Add(new ListItem("0", "0"));
            ddl.Items.Add(new ListItem("1", "1"));
            ddl.Items.Add(new ListItem("2", "2"));
            ddl.Items.Add(new ListItem("3", "3"));
            ddl.Items.Add(new ListItem("4", "4"));
            ddl.Items.Add(new ListItem("5", "5"));
            ddl.Items.Add(new ListItem("6", "6"));
            ddl.Items.Add(new ListItem("7", "7"));
            ddl.Items.Add(new ListItem("8", "8"));
            ddl.Items.Add(new ListItem("9", "9"));
            ddl.Items.Add(new ListItem("10", "10"));
            bool enabled = true;
            if (item["Disabled"] != null)
                enabled = !(bool)item["Disabled"];
            if (item["MatcheDate"] != null)
            {
                var MatcheDate = (DateTime)item["MatcheDate"];
                MatcheDate = MatcheDate;
                if (MatcheDate.AddHours(-2) < DateTime.Now)
                    enabled = false;
            }
            if (itemsUserMatches.Count == 64)
                if (itemsUserMatches[matchIndex - 1][scoreTeam] != null)
                    ddl.SelectedValue = itemsUserMatches[matchIndex - 1][scoreTeam].ToString();

            if (!string.IsNullOrEmpty(viewUser))
                enabled = false;

            
            ddl.Enabled = enabled;
            return ddl;
        }

        void Save_Click(Object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Site.RootWeb;
            SPUser user = web.CurrentUser;
            var list = web.Lists["HaviviUserMatches"];
            var query = new SPQuery
                            {
                                Query = string.Format("<Where><Eq><FieldRef Name=\"User\" /><Value Type=\"Text\">{0}</Value></Eq></Where><OrderBy><FieldRef Name=\"Matche\" Ascending=\"True\" /></OrderBy>", user.LoginName),
                                RowLimit = 100
                            };
            var items = list.GetItems(query);
            if (items.Count == 0)
            {
                for (int i = 1; i <= 64; i++)
                {
                    var teamScore1 = (DropDownList)FindControl("HaviviTeamScore1_" + i);
                                            var item = list.Items.Add();
                        item["Match"] = i;
                        item["User"] = user.LoginName;
                        item["DispName"] = user.Name;
                        if (teamScore1.SelectedValue != string.Empty)
                            item["ScoreTeamA"] = (teamScore1.Enabled) ? int.Parse(teamScore1.SelectedValue):99;
                        var teamScore2 = (DropDownList)FindControl("HaviviTeamScore2_" + i);
                        if (teamScore2.SelectedValue != string.Empty)
                            item["ScoreTeamB"] = (teamScore1.Enabled)?int.Parse(teamScore2.SelectedValue):99;
                        item.Update();
                    
                }
            }
            else
            {
                for (int i = 1; i <= 64; i++)
                {
                    var teamScore1 = (DropDownList)FindControl("HaviviTeamScore1_" + i);
                    
                    var item = items[i - 1];
                        item["Match"] = i;
                        item["User"] = user.LoginName;
                        item["DispName"] = user.Name;
                        if (teamScore1.SelectedValue != string.Empty)
                            item["ScoreTeamA"] = (teamScore1.Enabled)?int.Parse(teamScore1.SelectedValue):99;
                        var teamScore2 = (DropDownList)FindControl("HaviviTeamScore2_" + i);

                        if (teamScore2.SelectedValue != string.Empty)
                            item["ScoreTeamB"] = (teamScore1.Enabled) ?int.Parse(teamScore2.SelectedValue):99;
                        item.Update();
                    
                }
            }

            list = web.Lists["WK 2014 Standings"];
            query = new SPQuery
                          {
                              Query = string.Format("<Where><Eq><FieldRef Name=\"Gebruiker\" /><Value Type=\"Text\">{0}</Value></Eq></Where>", user.LoginName),
                              RowLimit = 1
                          };
            items = list.GetItems(query);

            if (items.Count == 0)
            {
                var item1 = list.Items.Add();
                item1["Score"] = 0;
                item1["Naam"] = user.Name;
                item1["Gebruiker"] = user.LoginName;
                item1.Update();
            }
        }

        private static string PopUp = "<a style=\"color: #676767\" accesskey=\"C\" id=\"{0}\"" +
                                                "oncontextmenu=\"ClkElmt(this); return false;\" onfocus=\"MMU_EcbLinkOnFocusBlur(byid('{0}'), this, true);\"" +
                                                "href=\"javascript:NewItem2(event, '{1}/{3}')\" serverclientid=\"{0}\">{2}</a>";

        private void GetLabels(SPWeb web)
        {
            Labels = new List<Label>();


            var list = web.Lists["WK 2014 Labels"];

            var items = list.GetItems();
            foreach (var c in from SPListItem item in items
                              select new Label
                                         {
                                             Key = item["Title"].ToString(),
                                             Value = item["Value"].ToString()
                                         })
            {
                Labels.Add(c);
            }

        }
    }

    public class Label
    {
        public string Key;
        public string Value;
    }

}
